using System.ComponentModel;

namespace BusinessRules
{
    public class EntityWithoutBase
    {
        public EntityWithoutBase()
        {
            Rules = new RulesExtension<EntityWithoutBase>(this);
        }
        public RulesExtension<EntityWithoutBase> Rules { get; private set; }

        public EntityComponent Component { get; set; }
        public string Text { get; set; }
        public int Price { get; set; }

        public int Count { get; set; }

        //[Email]
        public int Total { get; set; }

        public void Api()
        {
            var brokenRules = Rules.BrokenRules;
            var valid = Rules.LastValidStatus;
            Rules.Validate();

            //Rules.Enable();
            //Rules.Disable();

            // Action
            Rules.AddActionRuleWithoutDependency(e => e.SetTotal(e.Count * e.Price));

            // Business rule
            Rules.AddBusinessRule(e => e.Count * e.Price, e => e.Total);

            // Validation rule
            Rules.AddValidationRule(e => e.Count > 0);

            // Validation rule options
            Rules.AddValidationRule(e => e.Total == e.Price * e.Count)
                .WithMessage("Incorrect total price.")
                .WithSeverity(BrokenRuleSeverity.Error);

            // Custom rules
            //Rules.AddEmailRule(e => e.Text);

            // Custom rules
            Rules.AddNipRule(e => e.Text)
                .WithMessage("Invalid NIP code.")
                .WithSeverity(BrokenRuleSeverity.Warning);
        }

        private void SetTotal(int total)
        {
            Total = total;
        }
    }
}